<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #day="{ record }">
        <div v-if='record?.type == 1'>
          <a-link href="link" status="danger" :hoverable="false" v-if='record?.dept.zc_day != 0 && record?.dept.zc_day < record?.day'>已超期（{{record?.day - record?.dept.zc_day}}天）</a-link>
          <a-link href="link"  :hoverable="false" v-else>{{record?.day}}</a-link>
        </div>
        <div v-else>
          <a-link href="link" status="danger" :hoverable="false" v-if='record?.dept.cc_day != 0 && record?.dept.cc_day < record?.day'>已超期（{{record?.day - record?.dept.cc_day}}天）</a-link>
          <a-link href="link"  :hoverable="false" v-else>{{record?.day}}</a-link>
        </div>
      </template>
      
      <template #zc_day="{ record }">
        <div>{{ record?.zc_day != 0?record?.zc_day + '天':'-'}}</div>
      </template>
      <template #warehouse_link="{ record }">
        <div>{{ record?.warehouse_link != null?record?.warehouse_link?.warehouse?.name + '-' + record?.warehouse_link?.warehouse?.horizontal_span:'-'}}</div>
      </template>
      
      <template #shengyu_day="{ record }">
        <!-- {{ record?.shengyu_day < 0?record?.zc_day + '天':'-'}} -->
        <div v-if='record?.shengyu_day < 0'>
          <a-tag color="red" >超出免费堆放：{{ -record?.shengyu_day }}天</a-tag>
        </div>
        <div v-else>
          <a-tag color="green" >剩余免费堆放：{{ record?.shengyu_day }}天</a-tag>
        </div>
      </template>
      
      <!-- <template #dept_id="{ record }">
        <a-tooltip :content="record?.dept.name">
          <div>{{ record?.dept.name}}</div>
        </a-tooltip>
      </template> -->
      <template #goods_id="{ record }">
        <a-tooltip :content="record?.goods_name">
          <div>{{ record?.goods_name}}</div>
        </a-tooltip>
      </template>
      
    </ma-crud>
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import predictionHold from '@/api/prediction/predictionHold'
import { Message } from '@arco-design/web-vue'
import tool from '@/utils/tool'
import * as common from '@/utils/common'

const crudRef = ref()


const numberOperation = (newValue, id, numberName) => {
  predictionHold.numberOperation({ id, numberName, numberValue: newValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}

const switchStatus = (statusValue, id, statusName) => {
  predictionHold.changeStatus({ id, statusName, statusValue }).then( res => {
    res.success && Message.success(res.message)
  }).catch( e => { console.log(e) } )
}


const options = reactive({
  id: 'flyos_prediction_hold',
  rowSelection: {
    showCheckedAll: true
  },
  searchLabelWidth:'0px',
  searchColNumber:6,
  pk: 'id',
  operationColumn: false,
  operationWidth: 160,
  formOption: {
    viewType: 'modal',
    width: 600
  },
  api: predictionHold.getList,
  recycleApi: predictionHold.getRecycleList,
  add: {
    show: false,
    api: predictionHold.save,
    auth: ['prediction:hold:save']
  },
  edit: {
    show: false,
    api: predictionHold.update,
    auth: ['prediction:hold:update']
  },
  delete: {
    show: false,
    api: predictionHold.deletes,
    auth: ['prediction:hold:delete'],
    realApi: predictionHold.realDeletes,
    realAuth: ['prediction:hold:realDeletes']
  },
  recovery: {
    show: false,
    api: predictionHold.recoverys,
    auth: ['prediction:hold:recovery']
  },
  import: {
    show: false,
    url: 'prediction/hold/import',
    templateUrl: 'prediction/hold/downloadTemplate',
    auth: ['prediction:hold:import']
  },
  export: {
    show: true,
    url: 'prediction/hold/export',
    auth: ['prediction:hold:export']
  },
  afterRequest: (params) => {
    params.map((v, i) => {
      if(v.type == 1){
        if(v.prediction.load_detail != null){
          v.chare_time = v.prediction.load_detail.over_time
        }
        v.car_no = v.prediction.car_no
        v.goods_id =  v.prediction.goods_id
        v.goods_name =  v.prediction.goods_name
        v.box = v.prediction.box
        v.box_type = v.prediction.box_type
        v.zc_day = v.contract.zc_day
        v.number = v.prediction.number
        v.weight = (v.prediction.weight - 0).toFixed(3)
        v.shengyu_day = v.contract.zc_day - v.day
      }else{
        if(v.prediction.load_detail != null){
          v.chare_time = v.prediction.load_detail.over_time
        }
        v.car_no = v.warehouseuserdetail.car_no
        v.goods_id =  v.warehouseuserdetail.goods_id
        v.goods_name =  v.warehouseuserdetail.goods_name
        v.box = v.warehouseuserdetail.box
        v.box_type = v.warehouseuserdetail.box_type
        v.zc_day = v.contract.cc_day
        v.number = v.warehouseuserdetail.number_yu
        v.weight = (v.warehouseuserdetail.weight_yu - 0).toFixed(3)
        v.shengyu_day = v.contract.cc_day - v.day
      }
    });
  },
})

const columns = reactive([
  {
    title: "",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入"
    }
  },
  {
    title: "公司名称",
    dataIndex: "dept_id",
    formType: "select",
    width:180,
    align:'center',
    search: true,
    dict: {
      // 远程通用接口请求，新版代码生成都有一个 remote 接口
      remote: "system/dept/remote",
      // 指定组件接收的props
      props: { label: "name", value: "id" },
      // 开启分页
      openPage: false,
      // 远程请求配置项
      remoteOption: {
        // 按用户名排序
        sort: { id: "desc" }, // 如果不指定排序方式，默认为正序排序
        // 设置查询的字段
        select: ["id", "name"],
        // 设置数据过滤
        filter: {
          type: ["=", 2],
          status: ['=',1]
        },
        // 关联模型
        relations: [],
      },
      translation: true
    },
  },
  {
    title: "堆放天数",
    dataIndex: "day",
    align:'center',
    width:160,
    formType: "input",
    search: true,
    searchPlaceholder:'请输入格式：1；或1-5',
    
  },
  {
    title: "免费堆放天数",
    dataIndex: "zc_day",
    align:'center',
    width:160,
    formType: "input",
   
  }, 
  {
    title: "剩余堆放天数",
    dataIndex: "shengyu_day",
    align:'center',
    width:160,
    formType: "input",
    sortable:{
      sortDirections: ['ascend','descend'], sorter: false 
    }
  },
  {
    title: "是否仓储",
    dataIndex: "type",
    align:'center',
    formType: "select",
    search: true,
    dict: {
      data: [
        {
          label: "非仓储",
          value: "1"
        },
        {
          label: "仓储",
          value: "2"
        }
      ],
      translation: true
    }
  },
  {
    title: "车号",
    dataIndex: "car_no",
    align:'center',
    width:180,
    formType: "input",
    search: true
  },
  {
    title: "品名",
    dataIndex: "goods_id",
    formType: "select",
    align:'center',
    width:180,
    search: true,
    dict: {
      url: "goods/manage/index",
      params:{
        page:1,
        pageSize:500
      },
      props: { label: "name", value: "id" },
    },
  },
  
  {
    title: "箱号",
    dataIndex: "box",
    align:'center',
    width:140,
    formType: "input",
    search: true
  },
  {
    title: "箱型箱类",
    dataIndex: "box_type",
    align:'center',
    width:140,
    formType: "input",
    search: true
  },
  {
    title: "堆放库位",
    dataIndex: "warehouse_link",
    align:'center',
    width:140,
    formType: "input",
    search: false
  },
  {
    title: "备注",
    dataIndex: "warehouse_link.remark",
    align:'center',
    width:140,
    formType: "input",
    search: false
  },
  {
    title: "件数",
    dataIndex: "number",
    align:'center',
    width:140,
    formType: "input",
    search: true
  },
  {
    title: "重量",
    dataIndex: "weight",
    align:'center',
    width:140,
    formType: "input",
    search: true
  },
  {
    title: "开始计时时间",
    dataIndex: "chare_time",
    formType: "range",
    align:'center',
    width:180,
    search: true,
    showTime: true
  },
  
  // {
  //   title: "免费暂存/仓储天数",
  //   dataIndex: "free_day",
  //   formType: "input",
  //   search: true
  // },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    width:180,
    hide: false,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  }
])
</script>
<script> export default { name: 'prediction:hold' } </script>